<template>
  <view class="container">
    <!-- 页面标题 -->
    <view class="header">
      <text class="title">修改学生信息</text>
    </view>


 

    <!-- 学生信息输入表单 -->
    <view class="form-container">
      <input type="text" placeholder="请输入学生名字" v-model="studentName" />
      <input type="text" placeholder="请输入家长电话" v-model="parentPhone" />
      
	  
	  <picker @change="schoolChange" :value="selectedSchoolIndex" :range="schools">
	    <view class="picker">
	  			<view class="uni-input">{{schools[selectedSchoolIndex]}}</view>
	    </view>
	  </picker>
	  
	  <picker @change="grade_Change"  :value="selected_grade_Index" :range="grade">
	    <view class="picker">
	      <view class="uni-input">{{grade[selected_grade_Index]}}</view>
	    </view>
	  </picker>
	  
	  <picker @change="class_Change" :value="selected_class_Index" :range="class_">
	    <view class="picker">
	      <view class="uni-input">{{class_[selected_class_Index]}}</view>
	    </view>
	  </picker>
	  
	  
	  <view class="joinbutton" @tap="update_student">
	  	<view  class="join_btn" >立刻提交</view>
	  </view>
	  
    </view>

 

 
  </view>
</template>






<script>
	import * as school_api from '@/api/school'
	import * as UserApi from '@/api/user'
	
	
export default {
  data() {
    return {
		  studentNid: null, // 用于存储传递过来的 nid
          studentName: '',
          parentPhone: '',
          selectedSchoolIndex: 0,
          selected_grade_Index: 0,
          selected_class_Index: 0,
          schools: ['请选择学校'], // 示例学校列表，您可以根据实际情况修改
          grade: ['请选择年级'], // 示例学校列表，您可以根据实际情况修改
          class_: ['请选择班别'], // 示例学校列表，您可以根据实际情况修改
		  department_structure: []
        };
  },
  onLoad(options) {
	  // 从 URL 查询参数中获取 nid

	  this.school_list();
	  
	  options.nid = 1342;
	  
	          if (options.nid) {
	              this.studentNid = options.nid;
	              console.log('接收到的 nid:', this.studentNid);
	  
	              // 根据 nid 获取学生详细信息
	              this.getStudentDetail();
	          } else {
	              // 如果没有接收到 nid，提示用户
	              uni.showToast({
	                  title: '未找到学生信息',
	                  icon: 'none'
	              });
	          }
			  
			  
  },
  onShow() 
  {
  	
	
  },
  methods: 
  {
	  
	  
	  // 获取当前student detail data
	  getStudentDetail() {
 
		console.log("getStudentDetail",this.studentNid)
		const params = {
			nid: this.studentNid
		}
		UserApi.children_data(params).then(res => {
			this.student = res.data
			this.parentPhone = res.data.phone
			this.studentName = res.data.name
			// this.schools[0]= res.data.school
			// this.grade[0]= res.data.grade
			// this.class_[0]= res.data.class_
			console.log('res.data',res.data);
			});

 
	  },
	  
	  
	 // 获取所有学校的结构
	 school_list() {
		 
		 console.log("go school_list")
	 	school_api.list().then(res => {
			
	 			// 假设res.info是一个JSON格式的字符串
	 			this.department_structure = res.info
	 		
	 			 let school_list= res.info.map(item => item.name);
	 			 console.log(school_list,"school_list")
	 			 this.schools.push(...school_list);
	 			 
				 //开始铺班级
				 
				 
				 
	 			 //console.log(this.schools,"this.schools")
	 			 //console.log(this.schools[2],"this.schools")
	 	})
	 },
	 
	 
	 
	 
	 schoolChange(e)  {
		 console.log("schoolChange")
	 		  
	 		  //清空一下班级
	 		  this.selected_grade_Index = 0;
	 		  this.selected_class_Index = 0;
 
	 		  
	 		  this.selectedSchoolIndex = e.target.value;
	 		  console.log('picker发送选择改变，携带key值为', e.detail.value)
	 		  this.selectedSchoolIndex = e.detail.value
	 		  
	 		  
	 		  let now_school = this.schools[this.selectedSchoolIndex]
	 		  console.log('当前选中',now_school);
	 		  let grade_list= [];
	 		  
	 		  //开始从结构中提出当前学校的年级
	 		  this.department_structure.forEach(function(item, index) {
	 		    if(item.name == now_school)
	 			{
	 				grade_list= item[now_school];
	 				console.log("当前年级",grade_list);
	 			}	
	 		  });
	 		  
	 	
	 		  let grade_name_list= grade_list.map(item => item.name);
	 		  this.grade.push(...grade_name_list);
	 		  
	     },
	 		  
	 		  
			  
 
	 		  
	 		  
	 		  grade_Change(e)  {
	 			  console.log(e.target.value)
	 		
	 		this.selected_class_Index = 0;
	 		  this.selected_grade_Index = e.target.value
	 			  
	 		  
	 		  let now_school = this.schools[this.selectedSchoolIndex]
	 		  let now_grade = this.grade[this.selected_grade_Index]
	 		  console.log('now_school',now_school);
	 		  console.log('now_grade',now_grade);
	 		  let grade_list= [];
	 		  let class_list= [];
	 		  
	 		  
	 		  //开始从结构中提出当前学校的年级
	 		  this.department_structure.forEach(function(item, index) {
	 		    if(item.name == now_school)
	 		  			{
	 		  				grade_list= item[now_school];
	 		  				console.log("当前年级",grade_list);
	 						grade_list.forEach(function(item, index) {
	 							if(item.name == now_grade)
	 								{
	 									class_list= item[now_grade];
	 									console.log("当前班别",class_list);
	 								}	
	 						});
	 		  			}
	 		  			
	 		  });
	 		  
	 		  	
	 			let class_name_list= class_list.map(item => item.name);
	 			this.class_.push(...class_name_list);
	 			
	 		   // this.class_= class_list.map(item => item.name);
	 		  
	 		  
	 		  
	     },
	 		  
	  
	 		  class_Change(e) {
	 			  console.log(e.target.value);
	 			  this.selected_class_Index = e.target.value;
	 		  },
		  
		  
	      update_student() {
	     const newStudent = {
	       member_nid:uni.getStorageSync('user_id'),
		   nid:this.studentNid,
	       name: this.studentName,
	       phone: this.parentPhone,
	       school: this.schools[this.selectedSchoolIndex],
	       grade: this.grade[this.selected_grade_Index],
	       class: this.class_[this.selected_class_Index],
	     };
	  
	        // 这里可以将新学生信息提交到后端或存储到本地数据中
	        console.log('新增学生:', newStudent);
	  
	       //检查newStudent是否为空
	       if(newStudent.name == '' || newStudent.phone == '' || newStudent.school == '' || newStudent.grade == '' || newStudent.class == '' || newStudent.school == '请选择学校' || newStudent.grade == '请选择年级' || newStudent.class == '请选择班级')
	       {
	       	uni.showToast({
	       	    title: '请填写完整信息',
	       	    icon: 'none'
	       	});
	       	return;
	       }
		   
		   //调用add接口，提交当前的数据
		   UserApi.children_edit(newStudent).then(res => {
		   	
		   	//补充根据返回信息判断问题
		   	if(res.code == 200)
		   	{
		   	
		   	uni.showToast({
		   	    title: '修改成功',
		   	    icon: 'success'
		   	});
	 
		   	
		   	uni.navigateTo({
		   		url:"/pages/user/student_list"
		   	})
		   	
		   	
		   	}
		   }).catch(err => {
		   	uni.showToast({
		   	    title: '添加失败',
		   	    icon: 'none'
		   	});
		   });
		   
        },
		}
};
</script>



<style scoped lang="scss">
	@import url("asset/style.css");
</style>
 